<template>
	<w-dailog
		v-model="dialogVisible"
		width="550px"
		:title="title"
		:showMoreBtn="false"
		@close="onRefresh"
	>
		<fx-el-upload
			:fileList="planUrls"
			:limit="1"
			type='excel'
			:notes="true"
			:fileSize='5'
			class="avatar-upload-content"
		></fx-el-upload>
		<template slot="footer">
			<el-button @click="onSearchError" v-if="errorMsg.lengh">查看原因</el-button>
			<el-button @click="onCancelClick">取消</el-button>
			<el-button type="primary" plain @click="doImport">导入</el-button>
		</template>
		<ImporterrorInfo ref='ImporterrorInfo' :errorMsg="errorMsg"></ImporterrorInfo>
	</w-dailog>
</template>
<script>
import ImporterrorInfo from './ImporterrorInfo.vue'
export default {
	name: 'importPlan',
	data () {
		return {
			title: '导入生产计划',
			dialogVisible: false,
			planUrls: [],
			errorMsg: []
		}
	},
	components: {
		ImporterrorInfo
	},
	methods: {
		open () {
			this.planUrls = []
			this.errorMsg = []
			this.dialogVisible = true
		},
		// 取消
		onCancelClick () {
			this.dialogVisible = false
		},
		onRefresh () {
			this.$emit('on-refresh')
		},
		onSearchError () {
			this.$refs.ImporterrorInfo.open()
		},
		doImport () {
			if (this.planUrls.length === 0) {
				this.$fxMessage.warning('请选择导入的文件')
				return false
			}
			const fileObj = this.planUrls[0]
			return this.$fxApi('schedule.importProgramPlan')({
				data: {
					uploadFile: fileObj
				}
			}).then(res => {
				this.dialogVisible = false
			}).catch(err => {
				if (err.data.errorList.length > 0) {
					this.errorMsg = err.data.errorList
					this.$refs.ImporterrorInfo.open()
				}
			})
		}
	}
}
</script>
<style lang="stylus" scoped>
@import '~$assets/stylus/varsty.styl'
.create-organ {
	flex: 1
	display: flex;
	flex-direction: column;
	overflow: auto;
}
</style>
